package TransactionImplementation

import (
	"time"
	"SalaryPayment/TransactionApplication"
	"SalaryPayment/DataBase"
	"SalaryPayment/Model"
)

type PaydayTransaction struct {
	itsPayDate time.Time
	itsPaychecks map[int]Model.Paycheck
	TransactionApplication.Transaction
}

func(pt *PaydayTransaction) Execute() {
	empIds := DataBase.PayrollDatabase.Default.GetAllEmployeeIds()

	for _, empid := range empIds {
		e := DataBase.PayrollDatabase.Default.GetEmployee(empid)

		if e != nil {
			if e.IsPayDate(pt.itsPayDate) {
				pc := Model.NewPaycheck(e.GetPayPeriodStartDate(pt.itsPayDate), pt.itsPayDate)
				pt.itsPaychecks[empid] = pc
				e.Payday(pc)
			}
		}	
	}
}

func(pt *PaydayTransaction) GetPaycheck(empId int) Model.Paycheck {
	if id, ok := pt.itsPaychecks[empId]; ok {
		return id
	} else {
		return nil
	}
}

func(pt *PaydayTransaction) GetPaycheckCount() int {
	return len(pt.itsPaychecks)
}